package com.lwj.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

//@Component
public class LoggerBeanLoadCostPostProcessor implements BeanPostProcessor {

    private static Logger logger = LoggerFactory.getLogger(LoggerBeanLoadCostPostProcessor.class);

    private static Map<String, Long> cost = new HashMap<>(10000);

    private static AtomicInteger beanCount = new AtomicInteger(0);


    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        logger.info("first load_spring_bean_cost_info,bean init beanName:{},begintime : {}",
                beanName,System.currentTimeMillis());
        cost.put(beanName, System.currentTimeMillis());
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {

        beanCount.incrementAndGet();
        if(cost.get(beanName) == null){
             logger.warn("first load_spring_bean_cost_info, beanCount : {} ,cost.get(beanName : {} ) is null",beanCount.get(),beanName);
        }else{
            logger.info("first load_spring_bean_cost_info,bean after beanCount : {},beanName:{},beanType :{}  before: {} ,cost : {}",beanCount.get(),beanName,
                    bean.getClass().getName(),cost.get(beanName), (System.currentTimeMillis()- cost.get(beanName)));
        }

        return bean;
    }
}
